package com.whale.Task;

import com.whale.entity.Orders;
import com.whale.entity.Reserve;
import com.whale.mapper.OrderMapper;
import com.whale.mapper.ReserveMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.List;

@Component
@Slf4j
public class ReserveTask {


    @Autowired
    private ReserveMapper reserveMapper;

    /**
     *处理超时预约的方法
     *
     */

    @Scheduled(cron = "0 * * * * ?")
    public void processTimeoutOrder() {

        log.info("定时处理超时预约:{}", LocalDateTime.now());
        LocalDateTime time = LocalDateTime.now().plusMinutes(-15);
        List<Reserve> reserveList = reserveMapper.getByStatusAndReserveTimeLT(Reserve.PENDING_PAYMENT, time);
        if(reserveList != null && reserveList.size()>0){
            for(Reserve reserve : reserveList){
                reserve.setStatus(Reserve.CANCELLED);
                reserve.setCancelReason("预约超时，自动取消");
                reserve.setCancelTime(LocalDateTime.now());
                reserveMapper.update(reserve);
            }
        }

    }

}
